package com.smartao.core;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.smartao.core.security.SmartaoUser;

public class UserContext {

	/**
	 * 获取当前用户
	 */
	@SuppressWarnings("unchecked")
	public static <User extends SmartaoUser> User getCurrentUser() {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		Authentication authentication = securityContext.getAuthentication();
		if (authentication == null) {
			return null;
        }
		Object principal = authentication.getPrincipal();
		if (principal == null) {
			return null;
		}
		if (SmartaoUser.class.isInstance(principal)) {
			return (User) principal;
		}
		return null;
	}
	
}
